package cn.yuanqiao.archive.quanzongjuanguanli.service.impl;

import cn.yuanqiao.archive.quanzongjuanguanli.domain.YqQzjjsHb;
import cn.yuanqiao.archive.quanzongjuanguanli.mapper.YqQzjjsMapper;
import cn.yuanqiao.archive.quanzongjuanguanli.service.IYqQzjjsService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;

/**
 * 全宗卷介绍Service业务层处理
 * 
 * @author sy
 * @date 2023-09-20
 */
@Service
public class YqQzjjsServiceImpl implements IYqQzjjsService
{
    @Autowired
    private YqQzjjsMapper yqQzjjsMapper;

//    /**
//     * 查询全宗卷介绍
//     *
//     * @param id 全宗卷介绍主键
//     * @return 全宗卷介绍
//     */
//    @Override
//    public YqQzjjs selectYqQzjjsById(String id)
//    {
//        return yqQzjjsMapper.selectYqQzjjsById(id);
//    }

    /**
     * 查询全宗卷介绍
     *
     * @param id 全宗卷介绍主键
     * @return 全宗卷介绍
     */
    @Override
    public YqQzjjsHb selectYqQzjjsById(String id) {
        YqQzjjsHb yqQzjjsHb = yqQzjjsMapper.selectYqQzjjsById(id);

        // 对需要解码的String属性进行Base64解码
//        yqQzjjsHb.setRole(decodeString(yqQzjjsHb.getRole()));
//        yqQzjjsHb.setDirectorList(decodeString(yqQzjjsHb.getDirectorList()));
//        yqQzjjsHb.setOrganization(decodeString(yqQzjjsHb.getOrganization()));
//        yqQzjjsHb.setCompleteness(decodeString(yqQzjjsHb.getCompleteness()));
//        yqQzjjsHb.setAppraisalSituation(decodeString(yqQzjjsHb.getAppraisalSituation()));
//        yqQzjjsHb.setSearchTool(decodeString(yqQzjjsHb.getSearchTool()));
        return yqQzjjsHb;
    }

    /**
     * 查询全宗卷介绍
     *
     * @param fondsId 全宗主键
     * @return 全宗卷介绍
     */
    @Override
    public YqQzjjsHb selectYqQzjjsByFondsId(String fondsId)
    {
        return yqQzjjsMapper.selectYqQzjjsByFondsId(fondsId);
    }

    /**
     * 查询全宗卷介绍列表
     * 
     * @param yqQzjjsHb 全宗卷介绍
     * @return 全宗卷介绍
     */
    @Override
    public List<YqQzjjsHb> selectYqQzjjsList(YqQzjjsHb yqQzjjsHb)
    {
        return yqQzjjsMapper.selectYqQzjjsList(yqQzjjsHb);
    }

//    /**
//     * 新增全宗卷介绍
//     *
//     * @param yqQzjjs 全宗卷介绍
//     * @return 结果
//     */
//    @Override
//    public int insertYqQzjjs(YqQzjjs yqQzjjs)
//    {
//        LoginUser user = SecurityUtils.getLoginUser();
//
//        if(user == null){
//            user = new LoginUser();
//            user.setUserId(1L);
//        }
//        yqQzjjs.setId(SnowflakeIdGenerator.generateId());
//        yqQzjjs.setCreatorId(user.getUserId());
//        yqQzjjs.setCreateTime(DateUtils.getNowDate());
//        yqQzjjs.setTenantCode(SecurityUtils.getTenantCode());
//        return yqQzjjsMapper.insertYqQzjjs(yqQzjjs);
//    }

    /**
     * 新增全宗卷介绍
     *
     * @param yqQzjjsHb 全宗卷介绍
     * @return 结果
     */
    @Override
    public int insertYqQzjjs(YqQzjjsHb yqQzjjsHb)
    {
        LoginUser user = SecurityUtils.getLoginUser();

        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        if(yqQzjjsMapper.selectYqQzjjsIsExist(String.valueOf(yqQzjjsHb.getFondsId()))!=0){
            yqQzjjsHb.setLastupdater(user.getUserId());
            yqQzjjsHb.setLastupdateTime(DateUtils.getNowDate());
            return yqQzjjsMapper.updateYqQzjjs(yqQzjjsHb);
        }else {
            yqQzjjsHb.setId(SnowflakeIdGenerator.generateId());
            yqQzjjsHb.setCreatorId(user.getUserId());
            yqQzjjsHb.setCreateTime(DateUtils.getNowDate());
            yqQzjjsHb.setTenantCode(SecurityUtils.getTenantCode());
            return yqQzjjsMapper.insertYqQzjjs(yqQzjjsHb);
        }
    }

//    /**
//     * 修改全宗卷介绍
//     *
//     * @param yqQzjjs 全宗卷介绍
//     * @return 结果
//     */
//    @Override
//    public int updateYqQzjjs(YqQzjjs yqQzjjs)
//    {
//        LoginUser user = SecurityUtils.getLoginUser();
//
//        if(user == null){
//            user = new LoginUser();
//            user.setUserId(1L);
//        }
//        yqQzjjs.setLastupdater(user.getUserId());
//        yqQzjjs.setLastupdateTime(DateUtils.getNowDate());
//        return yqQzjjsMapper.updateYqQzjjs(yqQzjjs);
//    }

    /**
     * 修改全宗卷介绍
     *
     * @param yqQzjjsHb 全宗卷介绍
     * @return 结果
     */
    @Override
    public int updateYqQzjjs(YqQzjjsHb yqQzjjsHb)
    {
        LoginUser user = SecurityUtils.getLoginUser();

        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        yqQzjjsHb.setLastupdater(user.getUserId());
        yqQzjjsHb.setLastupdateTime(DateUtils.getNowDate());
        return yqQzjjsMapper.updateYqQzjjs(yqQzjjsHb);
    }

    /**
     * 批量删除全宗卷介绍
     * 
     * @param ids 需要删除的全宗卷介绍主键
     * @return 结果
     */
    @Override
    public int deleteYqQzjjsByIds(String[] ids)
    {
        return yqQzjjsMapper.deleteYqQzjjsByIds(ids);
    }

    /**
     * 删除全宗卷介绍信息
     * 
     * @param id 全宗卷介绍主键
     * @return 结果
     */
    @Override
    public int deleteYqQzjjsById(String id)
    {
        return yqQzjjsMapper.deleteYqQzjjsById(id);
    }

    // 编码字符串为 BLOB
    public Blob encodeStringToBlob(String data) throws SQLException {
        if (data != null) {
            byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
            return new javax.sql.rowset.serial.SerialBlob(bytes);
        }
        return null;
    }

    // 解码 BLOB为字符串
    public String decodeBlobToString(Blob blob) throws SQLException {
        if (blob != null) {
            byte[] bytes = blob.getBytes(1, (int) blob.length());
            return new String(bytes, StandardCharsets.UTF_8);
        }
        return null;
    }

    private String decodeString(String encodedData) {
        if (encodedData != null) {
            byte[] encodedBytes = hexStringToString(encodedData).getBytes();
            return new String(encodedBytes, StandardCharsets.UTF_8);
        }
        return null;
    }

    private String hexStringToString(String hexString) {
        int length = hexString.length();
        if (length % 2 != 0) {
            throw new IllegalArgumentException("Invalid hex string");
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i += 2) {
            String hex = hexString.substring(i, i + 2);
            int decimal = Integer.parseInt(hex, 16);
            sb.append((char) decimal);
        }
        return sb.toString();
    }
}
